﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace Solution
{
    /// <summary>
    /// json 序列化
    /// </summary>
    public static class Serializer
    {
        #region 公共方法

        /// <summary>
        /// JSON转对象，返回空默认
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T TryFromJson<T>(string json)
            where T : class, new()
        {
            try
            {
                var value = JsonConvert.DeserializeObject<T>(json);
                return value;
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// JSON转对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T FromJson<T>(string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }

        /// <summary>
        /// 将字符串转换为 动态对象
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static dynamic FromJson(string json)
        {
            try
            {
                //JArray ja = (JArray)JsonConvert.DeserializeObject(json);  //对象集合
                //if (ja.Count > 0)
                //{
                //    JObject o = (JObject)ja[0];  //若存在，取第一个元素
                //    return o;
                //}

                return JsonConvert.DeserializeObject(json);
            }
            catch (System.Exception ex)
            {

                throw new System.Exception(ex.Message);
            }

            return null;
        }
        /// <summary>
        /// 数据实体转化为JSON数据
        /// </summary>
        /// <param name="obj">要转化的数据实体</param>
        /// <returns>JSON格式字符串</returns>
        public static string ToJson<T>(T obj)
        {
            if (obj != null)
            {
                return JsonConvert.SerializeObject(obj);
            }
            return string.Empty;
        }


        public static T FromXml<T>(string xml)
        {
            var serializer = new XmlSerializer(typeof(T));
            using (var stream = new MemoryStream())
            {
                var writer = new StreamWriter(stream);
                writer.Write(xml);
                writer.Flush();
                stream.Position = 0;
                T instance = (T)serializer.Deserialize(stream);
                writer.Close();
                return instance;
            }
        }


        public static string ToJson(object entity)
        {
            var converter = new IsoDateTimeConverter();
            converter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            var serializer = new JsonSerializer();
            serializer.Converters.Add(converter);

            var sb = new StringBuilder();
            serializer.Serialize(new JsonTextWriter(new StringWriter(sb)), entity);
            return sb.ToString();
        }

        public static string ToXml<T>(T obj)
        {
            var serializer = new XmlSerializer(typeof(T));
            using (var stream = new MemoryStream())
            {
                serializer.Serialize(stream, obj);
                stream.Position = 0;
                var reader = new StreamReader(stream);
                var xml = reader.ReadToEnd();
                reader.Close();
                return xml;
            }
        }




        /// <summary>
        /// 将字符串转换为 动态对象集合
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static JArray JsonToObjectArray(string json)
        {
            try
            {
                JArray ja = (JArray)JsonConvert.DeserializeObject(json);  //对象集合
                return ja;
            }
            catch (System.Exception ex)
            {

                throw new System.Exception(ex.Message);
            }
            return null;
        }





        #endregion 公共方法
    }
}